package com.zd.springcloud.tx;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 使用的是数据本身的事务
 */
public class BianpingShiWu {
    public static void main(String[] args) throws Exception{

        String url = "jdbc:mysql://47.101.218.65:3306/aaa?serverTimezone=UTC&useSSL=false";

        String username = "root";
        String password = "root";
        //
        Connection connection = DriverManager.getConnection(url,username,password);



        connection.setAutoCommit(false); //开启事务


        try{
            //张三减钱
            String sql = "update user set money = money - 60 where id = 1";

            connection.prepareStatement(sql).execute();

            //李四加钱
            String sql1 = "update user set money = money + 60 where id = 2";
            int s = 10 / 0;
            connection.prepareStatement(sql1).execute();
            connection.commit();

        }catch (Exception e){
            System.out.println("出现了异常");
            connection.rollback();
        }finally {
            connection.close();
        }

    }
}
